import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../base/base_app.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/9/25.
///
/// gongzhonghao biglead
/// https://study.163.com/instructor/1021410098.htm
/// https://blog.csdn.net/zl18603543572
/// https://www.toutiao.com/c/user/token/MS4wLjABAAAAYMrKikomuQJ4d-cPaeBqtAK2cQY697Pv9xIyyDhtwIM/
///
///
///

//应用入口
void main() {
  ///启动根目录
  runApp(BaseApp(
    homeWidget: Example410(),
  ));
}

class Example410 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ExampleState();
  }
}

class _ExampleState extends State<Example410> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("图片浏览器"),
      ),
      backgroundColor: Colors.white,
      body: Center(
        child: buildClipOval(),
      ),
    );
  }

  // ClipRRect 将 child 剪裁为圆角矩形

  // ClipPath 将 child 按照给定的路径进行裁剪
  // CustomClipper 并不是一个widget，但是使用CustomClipper可以绘制出任何我们想要的形状

  // ClipOval 如果 child 为正方形时剪裁之后是圆形，如果 child 为矩形时，剪裁之后为椭圆形
  buildClipOval() {
    return ClipOval(
      child: Image.asset(
        "assets/images/loginbg.png",
        width: 100,
        height: 100,
      ),
    );
  }

  //ClipRect 将 child 剪裁为给定的矩形大小
  buildClipRect() {
    return ClipRect(
      /// 自定义裁剪路径
      clipper: ClipperPath(),
      child: Container(
        width: 100,
        height: 100,
        color: Colors.blue,
      ),
    );
  }


  buildClipRRect() {
    return ClipRRect(
      clipBehavior: Clip.antiAlias,//搞锯齿
      //对应的圆角
      borderRadius: const BorderRadius.all(Radius.circular(10)),
      child: Container(
        width: 100,
        height: 100,
        color: Colors.blue,
      ),
    );
  }

  buildTriangle() {
    return ClipPath(
      clipper: TriangleClipper(),
      child: Image.asset(
        "assets/images/loginbg.png",
        width: 100,
        height: 100,
      ),
    );
  }
}

///代码清单 4-9-1
///lib/code/code4/example_410_imageshow_page.dart
class ClipperPath extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    //自定义一个矩形
    return new Rect.fromLTRB(
      100,
      10,
      size.width,
      size.height,
    );
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    return true;
  }
}
///代码清单 4-9-2
///lib/code/code4/example_410_imageshow_page.dart
class TriangleClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    final path = Path()
      ..moveTo(0.0, size.height)
      ..lineTo(size.width, size.height)
      ..lineTo(size.width / 2, size.height / 2)
      ..close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
